/// Rust编程之道第一章第一节代码实现
///
/// 实现N次调用
pub fn start() {
    let mut i = 32;
    5.times(move || {
        i += 1;
        println!("{}", i);
    })
    .run();

    // ps: 还不如直接 for 循环调用...
}

trait Times<T> {
    fn times(&self, call: T) -> TimeRun<T>;
}

impl<T> Times<T> for i32 {
    fn times(&self, call: T) -> TimeRun<T> {
        TimeRun::new(*self, call)
    }
}

struct TimeRun<T> {
    call: T,
    times: i32,
}

impl<T> TimeRun<T> {
    fn new(times: i32, call: T) -> Self {
        Self { times, call }
    }
}

impl<T: FnMut()> TimeRun<T> {
    fn run(&mut self) {
        (0..self.times).for_each(|_| {
            (self.call)();
        });
    }
}

#[cfg(test)]
mod tests {

    #[test]
    fn test_first() {
        super::start();
    }
}
